package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.BuSysPerson;
import com.ruoyi.common.core.domain.entity.Person;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IPersonService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBuSysPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务系统对应人员信息Controller
 * 
 * @author ess
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/buSysPerson")
public class BuSysPersonController extends BaseController
{
    @Autowired
    private IBuSysPersonService buSysPersonService;

    @Autowired
    private IPersonService personService;

    /**
     * 查询业务系统对应人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:buSysPerson:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuSysPerson buSysPerson)
    {
        startPage();
        List<BuSysPerson> list = buSysPersonService.selectBuSysPersonList(buSysPerson);
        return getDataTable(list);
    }

    /**
     * 导出业务系统对应人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:buSysPerson:export')")
    @Log(title = "业务系统人员信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BuSysPerson buSysPerson)
    {
        List<BuSysPerson> list = buSysPersonService.selectBuSysPersonList(buSysPerson);
        ExcelUtil<BuSysPerson> util = new ExcelUtil<BuSysPerson>(BuSysPerson.class);
        return util.exportExcel(list, "业务系统人员信息数据");
    }

    /**
     * 获取业务系统对应人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:buSysPerson:query')")
    @GetMapping(value = "/{buPerId}")
    public AjaxResult getInfo(@PathVariable("buPerId") String buPerId)
    {
        return AjaxResult.success(buSysPersonService.selectBuSysPersonByBuPerId(buPerId));
    }

    /**
     * 新增业务系统对应人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:buSysPerson:add')")
    @Log(title = "业务系统人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuSysPerson buSysPerson)
    {
//        sysPersonName  姓名-8位生日  查询本系统是否有此人
       if (StringUtils.isNotEmpty(buSysPerson.getSysPersonName())) {

           if (!buSysPerson.getSysPersonName().contains("-")) {
               return AjaxResult.error("新增业务系统人员'" + buSysPerson.getSysPersonName() + "'失败，本系统人员格式错误，请重新输入");
           }

           Person person = new Person();
           person.setPersonName(buSysPerson.getSysPersonName().split("-")[0]);
           person.setBirNum(buSysPerson.getSysPersonName().split("-")[1]);
           List<Person> people = personService.selectPersonList(person);
           if (people != null&&people.size()>0) {
               buSysPerson.setPersonId(people.get(0).getPersonId());
           } else {
               return AjaxResult.error("新增业务系统人员'" + buSysPerson.getSysPersonName() + "'失败，此人员不存在");
           }

        }

        if (StringUtils.isNotEmpty(buSysPerson.getBuSysPerId()) && UserConstants.NOT_UNIQUE.equals(buSysPersonService.checkBuSysPerUnique(buSysPerson.getBuSysPerId()))) {
            return AjaxResult.error("新增业务系统人员'" + buSysPerson.getSysPersonName() + "'失败，业务系统人员ID已存在");
        }

        if (StringUtils.isNotEmpty(buSysPerson.getPersonId()) && UserConstants.NOT_UNIQUE.equals(buSysPersonService.checkBuSysPerUniqueByPersonId(buSysPerson.getPersonId()))) {
            return AjaxResult.error("新增业务系统人员'" + buSysPerson.getSysPersonName() + "'失败，本系统人员已存在");
        }



        return toAjax(buSysPersonService.insertBuSysPerson(buSysPerson));
    }

    /**
     * 修改业务系统对应人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:buSysPerson:edit')")
    @Log(title = "业务系统人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuSysPerson buSysPerson)
    {
        if (StringUtils.isNotEmpty(buSysPerson.getBuSysPerId()) && UserConstants.NOT_UNIQUE.equals(buSysPersonService.checkBuSysPerUnique(buSysPerson.getBuSysPerId()))) {
            BuSysPerson buSysPersonOld = buSysPersonService.selectBuSysPersonByBuPerId(buSysPerson.getBuPerId());
            if (!buSysPersonOld.getBuSysPerId().equals(buSysPerson.getBuSysPerId())  ) {
                return AjaxResult.error("修改业务系统人员'" + buSysPerson.getSysPersonName() + "'失败，业务系统人员ID已存在");
            }
        }

        return toAjax(buSysPersonService.updateBuSysPerson(buSysPerson));
    }

    /**
     * 删除业务系统对应人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:buSysPerson:remove')")
    @Log(title = "业务系统人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{buPerIds}")
    public AjaxResult remove(@PathVariable String[] buPerIds)
    {
        return toAjax(buSysPersonService.deleteBuSysPersonByBuPerIds(buPerIds));
    }
}
